//
//  ReadListViewController.m
//  Leisure
//
//  Created by 大展 on 16/6/29.
//  Copyright © 2016年 大展. All rights reserved.
//

#import "ReadListViewController.h"

@interface ReadListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UISegmentedControl *segentedmControl; // 分段视图控制器
@property (weak, nonatomic) IBOutlet UITableView *newestTableView;  // 最新列表
@property (weak, nonatomic) IBOutlet UITableView *hotTaleView;      // 最热列表
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;      // 滚动视图
@property (nonatomic, assign)NSInteger newStart; // 最新列表的数据起始位置
@property (nonatomic, assign)NSInteger newLimit; // 最新列表的数据的条数
@property (nonatomic, assign)NSInteger hotStart; // 最热列表的数据起始位置
@property (nonatomic, assign)NSInteger hotLimit; // 最热列表的数据的条数
@property (nonatomic, strong)NSMutableArray *newestDataSource; // 最新列表数据源
@property (nonatomic, strong)NSMutableArray *hotDataSource;    // 最热列表数据源


@end

@implementation ReadListViewController

- (NSMutableArray *)newestDataSource {

    if (!_newestDataSource) {

        _newestDataSource = [NSMutableArray array];
    }
    return _newestDataSource;
}

- (NSMutableArray *)hotDataSource {

    if (!_hotDataSource) {

        _hotDataSource = [NSMutableArray array];
    }
    return _hotDataSource;
}

- (UISegmentedControl *)segentedmControl {

    if (!_segentedmControl) {

        _segentedmControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        // 插入标签
        [_segentedmControl insertSegmentWithTitle:@"最新" atIndex:0 animated:YES];
        [_segentedmControl insertSegmentWithTitle:@"最热" atIndex:1 animated:YES];
        // 添加点击事件
        [_segentedmControl addTarget:self action:@selector(clickSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    }
    return _segentedmControl;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor blueColor];
    self.navigationItem.titleView = self.segentedmControl;

    // 注册Cell
//    [self.newestTableView registerClass:[BaseCell class] forCellReuseIdentifier:@"ReadListModel_cell"];
//    [self.hotTaleView registerClass:[BaseCell class] forCellReuseIdentifier:@"ReadListModel_cell"];
    [self.newestTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];
    [self.hotTaleView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];

    // 数据请求
    [self loadNewestTableViewDataSourceWithStart:0 limit:20];
    [self loadHotTableViewDataSourceWithStart:0 limit:20];

    // 添加下拉刷新
    self.newestTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{

        [self.newestDataSource removeAllObjects];
        self.newStart = 0;

        [self loadNewestTableViewDataSourceWithStart:0 limit:20];
    }];
    self.hotTaleView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{

        [self.hotDataSource removeAllObjects];
        self.hotStart = 0;

        [self loadHotTableViewDataSourceWithStart:0 limit:20];
    }];
    // 添加上拉加载
    self.newestTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{

        self.newStart +=20;
        [self loadNewestTableViewDataSourceWithStart:self.newStart limit:20];
    }];
    self.hotTaleView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{

        self.hotStart +=20;
        [self loadHotTableViewDataSourceWithStart:self.hotStart limit:20];
    }];
}


// 分段试图控制器的点击事件
- (void)clickSegmentedControl:(UISegmentedControl *)sender {

    if (sender.selectedSegmentIndex == 0) {

        [self.scrollView setContentOffset:CGPointZero animated:YES];
    }else {

        [self.scrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
    }
}

#pragma mark -----数据请求------
// 加载最新列表的数据
- (void)loadNewestTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit {

    [RequestManager fecthDataWithUrl:kReadListUrl parameter:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"addtime"} mehtod:@"POST" compelet:^(NSData *data) {

        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {

            ReadListModel *model = [[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];

            [self.newestDataSource addObject:model];
        }
    } updateUI:^{

        NSLog(@"update");

        // 结束刷新
        [self.newestTableView.mj_header endRefreshing];
        [self.newestTableView.mj_footer endRefreshing];
        [self.newestTableView reloadData];
    }];
}
// 加载最热列表的数据
- (void)loadHotTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit {
    [RequestManager fecthDataWithUrl:kReadListUrl parameter:@{@"typeid":@(self.typeID),@"start":@(self.hotStart),@"limit":@(self.hotLimit),@"sort":@"hot"} mehtod:@"POST" compelet:^(NSData *data) {

        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {

            ReadListModel *model = [[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.hotDataSource addObject:model];
        }

    } updateUI:^{

        [self.hotTaleView.mj_header endRefreshing];
        [self.hotTaleView.mj_footer endRefreshing];
        [self.hotTaleView reloadData];
    }];
}
#pragma mark -----ScrollViewDelegate-----
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    // 判断滚动的是不是你要滚的
    if (scrollView == self.scrollView) {

        if (self.scrollView.contentOffset.x == 0) {

            self.segentedmControl.selectedSegmentIndex = 0;
        }else if (self.scrollView.contentOffset.x == kWidth)

            self.segentedmControl.selectedSegmentIndex = 1;
    }
}

#pragma mark -----TableVideDelegate------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.newestTableView) {

        return self.newestDataSource.count;
    }else {

        return self.hotDataSource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // 创建一个Model来接受
    ReadListModel *model = [[ReadListModel alloc]init];
    if (tableView == self.newestTableView) {

        model = self.newestDataSource[indexPath.row];
    }else {

        model = self.hotDataSource[indexPath.row];
    }

    // 工厂模式产出来的Cell
    BaseCell *cell = [FactoryMode productCellWithModel:model tableView:tableView indexPath:indexPath];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    ReadListInfoViewController *readLIstInfoVC = [[ReadListInfoViewController alloc]init];
    if (tableView == self.newestTableView) {

        readLIstInfoVC.contentID = [self.newestDataSource[indexPath.row] ID];
    }else {
        readLIstInfoVC.contentID = [self.hotDataSource[indexPath.row] ID];
    }
    [self.navigationController pushViewController:readLIstInfoVC animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
